<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.util.*" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Student Portal</title>
<link href="style/css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
String txt=null;
String name = null;
String password = null;
if(request.getParameter("password") != null && request.getParameter("username") != null){
if(request.getParameter("password").equals("") || request.getParameter("username").equals("")) {
out.println("<font color=red>Enter login or password!!!</font>");
} else {
name = request.getParameter("username");
password = request.getParameter("password");

JSONObject obj=new JSONObject();
obj.put("login",name);
obj.put("password",password);

FacadeGuest fac = new FacadeGuest();
String param = obj.toString();

param = fac.authorization(param);
if ( param !=null ){ 
session.setAttribute("username", name);
JSONParser parser=new JSONParser();
Object o=parser.parse(param);
JSONObject jso = (JSONObject) o;
session.setAttribute("type",jso.get("type").toString());
if(("student").equals(session.getAttribute("type"))){
	String fullname=jso.get("stud_last_name").toString()+" "+jso.get("stud_first_name").toString();
	session.setAttribute("fullname",fullname);
	}
if(("professor").equals(session.getAttribute("type"))){
    String fullname=jso.get("prof_first_name").toString()+" "+jso.get("patronymic").toString();
	session.setAttribute("fullname",fullname);
	}
session.setAttribute("user_id", jso.get("user_id").toString());
response.sendRedirect("index.jsp");
}else{
txt ="<font color=red>Пользователь не найден!</font>";
} 

}
}
 
%>
<div class="main">
	<div class="wrapper">
    	<div class="user-panel">
    	
        	<div class="data">

        	<form action = "login.jsp" method = "POST">
            <p class="login"><span>Логин:</span><input type="text" name="username" value=""></p>
            <p class="pass"><span>Пароль:</span><input type="password" name="password" value="" ></p>
            <p class="submit"><input type="submit" value="Войти"></p>
            
<%
 if(txt !=null)
{
out.print(txt);
} 
%>
            
            </form>
            
         
            
            </div>
        </div>    

		<div class="cont">
		
        </div>
        
        <div class="cont-body">
        <font face="BIP" size="+3"> Student Learning Portal of VRN </font>
        </div>

    </div>
</div>

</body>
</html>